<?php
require_once("util.php");

$qry = $_GET['query'];
$display= $_GET['display'];

$result = file_get_contents("http://openapi.naver.com/search?key=f28f29afc3a3a0a0b09f6f64175d18ed&target=news&query="
			    . $qry ."&display=" . $display ."&start=1&sort=sim");
$obj = simplexml_load_string($result);

$j = "[";

$length = sizeof($obj->channel->item);
$iter = 0;

foreach($obj->channel->item as $item) {
  $iter++;
  $j .= '{"title":"' . json_esc($item->title) .
         '","link":"' .$item->link.
    '","desc":"'. json_esc($item->description) .
    '","pub_date":"'. $item->pubDate . '"}';
         
  if ($iter < $length)
    $j .= ",";
}
$j .= "]";
echo $j;
?>
